﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyDuAn.BUS;
using System.Data.SqlClient;
using System.Collections;
using QuanLyDuAn.DTO;

namespace QuanLyDuAn.GUI
{
    public partial class FrmLogin : DevComponents.DotNetBar.Office2007RibbonForm
    {
        ArrayList list, listInterviewer;
        AdminController adminControl = new AdminController();
        InterviewerController interviewerControl = new InterviewerController();
        Admin admin = new Admin();
        public FrmLogin()
        {
            InitializeComponent();
           
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        public int LoginAdmin()
        {
            int check =0;
            list = adminControl.LoadAdmin();
            String username = txtUsername.Text;
            String password = txtPassword.Text;
            foreach (Admin i in list)
            {
                String user = i.userName;
                String pass = i.password;
                if (username.CompareTo(user) == 0 && password.CompareTo(pass) == 0)
                {
                    check = 1;
                    break;
                }
                else
                {
                    check = 2;
                }
            }
            return check;
        }
        public int LoginInterviewer()
        {
            int check = 0;
            listInterviewer = interviewerControl.LoginInterviewer();
            String username = txtUsername.Text;
            String password = txtPassword.Text;
            foreach (Interviewer i in listInterviewer)
            {
                String user = i.itvUserName;
                String pass = i.itvPassword;
                if (username.CompareTo(user) == 0 && password.CompareTo(pass) == 0)
                {
                    check = 1;
                    break;
                }
                else
                {
                    check = 2;
                }
            }
            return check;
        }
        public void Login() {
                    String username = txtUsername.Text;
                    if (LoginAdmin()==1)
                    {
                        FrmMain frm = new FrmMain();
                        frm.Visible = true;
                        frm.lbUser.Text = "Welcome " + username.ToUpper() + " to Sytem !";
                        MessageBox.Show("Welcome "+username.ToUpper()+ " to System !");
                        frm.ShowControl();
                        frm.frmChangePass.txtusername.Text = username;
                        frm.permission = 1;
                        frm.Show();
                        this.Visible = false;
                    }
                    else if (LoginInterviewer() == 1)
                    {
                        FrmMain frm = new FrmMain();
                        frm.Visible = true;
                        frm.lbUser.Text = "Welcome " + username.ToUpper() + " to Sytem !";
                        MessageBox.Show("Welcome "+username.ToUpper()+ " to System !");
                        frm.ShowControlForInterviewer();
                        frm.frmChangePass.txtusername.Text = username;
                        
                        frm.permission = 2;
                        frm.Show();
                        this.Visible = false;
                    }
                    else
                    {
                        MessageBox.Show("Please enter user and password again !");
                        txtPassword.Text = "";
                    }

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.Login();
            
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {

        }
 
    }
}
